package com.aisile.base.controller;

import com.aisile.base.pojo.Label;
import com.aisile.base.service.LabelService;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @标题 LabelContorller
 * @作者 Mr.chen
 * @创建日期 2019/4/17 21:37
 * @版本号 1.0
 **/
@RestController
@RequestMapping("/label") // 资源
@CrossOrigin
public class LabelContorller {
    @Autowired
    private LabelService labelService;

    /**
     * 查询全部
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public Result findAll(){
        List<Label> all = labelService.findAll();
        return new Result(true, StatusCode.OK,"查询成功",all);
    }

    /**
     * 添加标签
     * @param label
     * @return
     */
    @RequestMapping(method = RequestMethod.POST)
    public Result add(@RequestBody Label label){
        labelService.add(label);
        return new Result(true, StatusCode.OK,"添加成功");
    }

    /**
     * 根据ID查询
     * @param labelId
     * @return
     */
    @RequestMapping(value = "/{labelId}",method = RequestMethod.GET)
    public Result findById(@PathVariable(name = "labelId") String labelId){
        Label label = labelService.findById(labelId);
        return new Result(true, StatusCode.OK,"查询成功",label);
    }

    /**
     * 根据ID修改
     * @param labelId
     * @param label
     * @return
     */
    @RequestMapping(value = "/{labelId}",method = RequestMethod.PUT)
    public Result update(@PathVariable(name = "labelId") String labelId,@RequestBody Label label){
        label.setId(labelId);
        labelService.update(label);
        return new Result(true, StatusCode.OK,"修改成功");
    }

    /**
     * 根据ID删除
     * @param labelId
     * @return
     */
    @RequestMapping(value = "/{labelId}",method = RequestMethod.DELETE)
    public Result deleteById(@PathVariable(name = "labelId") String labelId){
        labelService.deleteById(labelId);
        return new Result(true, StatusCode.OK,"修改成功");
    }
}
